use chrono::{DateTime, Local, NaiveDateTime, TimeZone};

use crate::{Error, Result};

pub fn naive_to_local(n: &NaiveDateTime) -> Result<DateTime<Local>> {
    match Local.from_local_datetime(n) {
        chrono::offset::LocalResult::Single(v) => Ok(v),
        chrono::offset::LocalResult::Ambiguous(v, _) => Ok(v),
        chrono::offset::LocalResult::None => Err(Error::chrono("无法解析日期时间")),
    }
}
pub fn parse(dt_str: &str) -> Result<DateTime<Local>> {
    let nd = NaiveDateTime::parse_from_str(dt_str, "%Y-%m-%d %H:%M:%S").map_err(Error::from)?;
    naive_to_local(&nd)
}

pub fn today_range() -> Result<(DateTime<Local>, DateTime<Local>)> {
    let now = Local::now();

    let start = now.format("%Y-%m-%d 00:00:00").to_string();
    let end = now.format("%Y-%m-%d 23:59:59").to_string();

    let start = parse(&start)?;
    let end = parse(&end)?;

    Ok((start, end))
}

pub fn default() -> DateTime<Local> {
    Local::now()
}

#[cfg(test)]
mod test {
    #[test]
    fn test_dt_parse() {
        let s = "2024-06-20 11:54:04";
        let dt = super::parse(s).unwrap();
        println!("{:?}", dt);
    }
    #[test]
    fn test_dt_today_range() {
        let (start, end) = super::today_range().unwrap();
        println!("{:?}, {:?}", start, end);
    }
}
